package com.ncmmall.application.business.express;

import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.express.Express;
import com.ncmmall.domain.business.express.ExpressRepository;
import com.ncmmall.domain.business.express.QExpress;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Longs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 物流管理
 *
 * @author likaikai
 * @date 2019-07-11 10:33
 */
@Service
@Transactional
public class ExpressApplication {

    @Autowired
    private ExpressRepository expressRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 创建物流
     *
     * @param news
     */
    public void create(Express express) {

        int count = queryChannelService.selectCount("select count(*) from mall_express where `name` = :name", ImmutableMap.of("name", express.getName()));
        Requires.isTrue(count == 0, "物流名称已存在");

        express.setStatus(1);
        express.setCreateTime(new Timestamp(System.currentTimeMillis()));

        expressRepository.save(express);
    }

    /**
     * 编辑物流
     *
     * @param param
     */
    public void modify(Express param) {

        Express express = queryChannelService.findOne(QExpress.express.id.eq(param.getId()), Express.class);
        Requires.notNull(express, "物流不存在");

        int count = queryChannelService.selectCount("select count(*) from mall_express where `name` = :name and id != :eid", ImmutableMap.of("name", express.getName(), "eid", express.getId()));
        Requires.isTrue(count == 0, "物流名称已存在");

        express.setName(param.getName());
        express.setExpressCompanyCode(param.getExpressCompanyCode());
        expressRepository.save(express);
    }

    /**
     * 上架
     *
     * @param id
     */
    public void enabled(long id) {

        Express express = queryChannelService.findOne(QExpress.express.id.eq(id), Express.class);
        Requires.notNull(express, "物流不存在");

        express.setStatus(2);
        expressRepository.save(express);
    }


    /**
     * 下架
     *
     * @param id
     */
    public void disable(long id) {

        Express express = queryChannelService.findOne(QExpress.express.id.eq(id), Express.class);
        Requires.notNull(express, "物流不存在");

        express.setStatus(1);
        expressRepository.save(express);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void remove(long id) {
        expressRepository.delete(id);
    }

    /**
     * 批量下架
     *
     * @param data
     */
    public void disables(String data) {
        List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(data).stream().map(Longs::tryParse).collect(Collectors.toList());
        for (Long id : ids) {
            disable(id);
        }
    }

    /**
     * 批量上架
     *
     * @param data
     */
    public void enables(String data) {
        List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(data).stream().map(Longs::tryParse).collect(Collectors.toList());
        for (Long id : ids) {
            enabled(id);
        }
    }
}
